package com.hitqz.robot.driver.schedule;

import cn.hutool.core.util.StrUtil;
import com.hitqz.robot.driver.service.NettyChannelService;
import com.hitqz.robot.driver.util.SourceEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author xupkun
 * @date 2024/11/26
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class CheckAliveSchedule {


    public static String ip = "";

    @Scheduled(fixedRate = 5000)
    public void reconnect() {
        if (StrUtil.isBlank(ip)) {
            return ;
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_MESSAGE_PUSH.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_MESSAGE_PUSH);
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_NAV.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_NAV);
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_OTHER.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_OTHER);
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_STATUS.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_STATUS);
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_CONTROLLER.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_CONTROLLER);
        }
        if (!NettyChannelService.checkAlive(SourceEnum.ROBOT_CONFIG.getSource())) {
            NettyChannelService.connect(ip, SourceEnum.ROBOT_CONFIG);
        }
        ////boolean connectRobot = NetUtil.ping(ip,3000);
        //if (disConnect) {
        //    // 全部断开
        //    log.info("存在ping超时或者是无法链接的情况，全部断开");
        //    //NettyChannelService.removeAll();
        //    if (StrUtil.isNotEmpty(ip)) {
        //
        //    }
        //}
    }


}
